package village.rainbow.slipin.uicontrol;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Typeface;
import android.graphics.drawable.GradientDrawable;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
 * @author Kwon
 */
public class RoundButton extends View {

	private String _labelText;
	public static final int TYPE_RED = 0;
	public static final int TYPE_BLACK = 1;
	public static final int TYPE_YELLOW = 2;
	public static final int TYPE_GREEN = 3;

	private boolean _widthWrapContent;
	private boolean _heightWrapContent;

	private int bgStartColor;
	private int bgEndColor;

	private int glowStartColor;
	private int glowEndColor;

	private int bgOverStartColor;
	private int bgOverEndColor;

	private int textShadowColor;
	private int textColor;

	public RoundButton(Context context) {
		this(context, null);
	}

	public RoundButton(Context context, AttributeSet attrs) {
		super(context, attrs);
		int attrIndex = 0;
		if(attrs != null){
			int attrCount = attrs.getAttributeCount();
			while(attrIndex < attrCount){
				if(attrs.getAttributeName(attrIndex).equals("text")){
					_labelText = attrs.getAttributeValue(attrIndex);
				}
				if(attrs.getAttributeName(attrIndex).equals("layout_width")){
					_widthWrapContent = attrs.getAttributeValue(attrIndex).equals("-2")?true:false;
				}
				if(attrs.getAttributeName(attrIndex).equals("layout_height")){
					_heightWrapContent = attrs.getAttributeValue(attrIndex).equals("-2")?true:false;
				}
				attrIndex++;
			}	
		}
		this.setBackgroundColor(Color.argb(0, 0, 0, 0));
		setStyle(TYPE_BLACK);
	}

	public void setText(String text){
		_labelText = text;
		this.invalidate();
	}

	public String getText(){
		return _labelText; 
	}

	public void setStyle(int colorType){
		if(colorType == TYPE_RED){

			bgStartColor = Color.rgb(255, 135, 139);
			bgEndColor = Color.rgb(211, 68, 73);

			glowStartColor = Color.rgb(255, 170, 176);
			glowEndColor = Color.rgb(255, 114, 119);

			bgOverStartColor = Color.rgb(172, 56, 60);
			bgOverEndColor = Color.rgb(255, 83, 91);

			textColor = Color.WHITE;
			textShadowColor = Color.rgb(142, 7, 7);

		}		
		else if(colorType == TYPE_BLACK){

			bgStartColor = Color.rgb(135, 135, 135);
			bgEndColor = Color.rgb(93, 93, 93);

			glowStartColor = Color.rgb(171, 171, 171);
			glowEndColor = Color.rgb(128, 128, 128);

			bgOverStartColor = Color.rgb(94, 94, 94);
			bgOverEndColor = Color.rgb(138, 138, 138);

			textColor = Color.WHITE;
			textShadowColor = Color.BLACK;
		}
		else if(colorType == TYPE_YELLOW){

			bgStartColor = Color.rgb(255, 242, 0);
			bgEndColor = Color.rgb(242, 231, 0);

			glowStartColor = Color.rgb(255, 250, 142);
			glowEndColor = Color.rgb(255, 248, 100);

			bgOverStartColor = Color.rgb(242, 231, 0);
			bgOverEndColor = Color.rgb(255, 242, 0);

			textColor = Color.rgb(193, 154, 107);
			textShadowColor = Color.rgb(142, 7, 7);
		}
		else if(colorType == TYPE_GREEN){
			
			bgStartColor = Color.rgb(217, 234, 70);
			bgEndColor = Color.rgb(217, 214, 50);

			glowStartColor = Color.rgb(237, 255, 100);
			glowEndColor = Color.rgb(237, 225, 70);

			bgOverStartColor = Color.rgb(217, 214, 50);
			bgOverEndColor = Color.rgb(217, 234, 70);

			textColor = Color.rgb(193, 154, 107);
			textShadowColor = Color.rgb(142, 7, 7);
		}
		
	}

	@SuppressLint("DrawAllocation")
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);

		Paint paint = new Paint();
		paint.setAntiAlias(true);
		int textHeight = 22;	

		int _width = this.getWidth();
		int _height = this.getHeight();

		if(_widthWrapContent){
			_width = _labelText.length() * 22 + 24;
		}
		if(_heightWrapContent){
			_height = 22 + 44;
		}

		if(!isClicked){
			
			float r = 12;
			float[] CORNERS = new float[] { r, r, r, r, r, r, r, r };
			int startColor = bgStartColor;
			int endColor = bgEndColor;
			int[] colors = new int[]{startColor, endColor};
			GradientDrawable bg = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colors);
			bg.setGradientType(GradientDrawable.RECTANGLE);
			bg.setCornerRadii(CORNERS);
			bg.setBounds(1, 1, _width - 1, _height - 1);
			bg.draw(canvas);

			int bStartColor = glowStartColor;
			int bEndColor = glowEndColor;
			int[] bColors = new int[]{bStartColor, bEndColor};
			GradientDrawable bg2 = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, bColors);
			bg2.setGradientRadius(GradientDrawable.RECTANGLE);
			bg2.setCornerRadii(CORNERS);
			bg2.setBounds(4, 3, _width - 4, _height/2);
			bg2.draw(canvas);
			
			TextPaint tp = new TextPaint(Paint.ANTI_ALIAS_FLAG);
			tp.setColor(textColor);
			tp.setTextSize(textHeight);
			tp.setTextAlign(Align.CENTER);
			tp.setShadowLayer(1, 1, 1, textShadowColor);
			tp.setTypeface(Typeface.MONOSPACE);

			float textCenterX = _width/2;
			float textCenterY = (_height + textHeight)/2 - 2;
			
			if(_labelText != null)
				canvas.drawText(_labelText, textCenterX, textCenterY, tp);

		}else{

			float r = 12;
			float[] CORNERS = new float[] { r, r, r, r, r, r, r, r };
			int startColor = bgOverStartColor;
			int endColor = bgOverEndColor;
			int[] colors = new int[]{startColor, endColor};
			GradientDrawable bg = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colors);
			bg.setGradientType(GradientDrawable.RECTANGLE);
			bg.setCornerRadii(CORNERS);
			bg.setBounds(1, 1, _width - 1, _height - 1);
			bg.draw(canvas);

			TextPaint tp = new TextPaint(Paint.ANTI_ALIAS_FLAG);
			tp.setColor(textColor);
			tp.setTextSize(textHeight);
			tp.setTextAlign(Align.CENTER);
			tp.setShadowLayer(1, 1, 1, textShadowColor);
			tp.setTypeface(Typeface.MONOSPACE);

			float textCenterX = _width/2;
			float textCenterY = (_height + textHeight)/2 - 2;
			
			if(_labelText != null)
				canvas.drawText(_labelText, textCenterX, textCenterY, tp);

		}

	}

	boolean isClicked = false;
	@Override
	public boolean onTouchEvent(MotionEvent event) {

		switch(event.getAction() & MotionEvent.ACTION_MASK){
		case MotionEvent.ACTION_DOWN :
			isClicked = true;
			break;
		case MotionEvent.ACTION_UP :
			isClicked = false;
			break;
		case MotionEvent.ACTION_CANCEL:
			isClicked = false;
			break;
		case MotionEvent.ACTION_POINTER_DOWN :
			isClicked = true;
			break;
		case MotionEvent.ACTION_POINTER_UP :
			isClicked = false;
			break;
		}
		this.invalidate();	
		return super.onTouchEvent(event);
	}

}

